/*
 * VentanaModificarPrograma.java
 *
 * Created on 7 de junio de 2007, 03:40 PM
 */

package everest3.materialBibliografico;

import everest3.Ayuda.MostrarAyuda; 
import java.awt.Frame; 
import java.awt.event.ActionEvent; 
import java.awt.event.FocusEvent; 
import java.awt.event.MouseEvent; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JDialog; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JSeparator; 
import javax.swing.JTextField;
import everest3.utils.Fecha;
import java.awt.Dimension;
import java.awt.Toolkit;
import tablasYBasesdeDatos.JDBCAdapter;
import tablasYBasesdeDatos.S15WorkingBackspace;

/**
 * Clase que administra la modificaci&oacute;n de los programas Acad&eacute;micos
 * @author JOSE LIBARDO MOLINA GALVIS
 */
public class VentanaModificarPrograma extends javax.swing.JDialog {
    
    /**
     * Creates new form VentanaModificarPrograma
     * @param parent 
     * @param modal 
     */
    public VentanaModificarPrograma(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        conectar();
        initComponents();         
        cargarPrograma();
        dimensionarPantalla();     
        
    }
    /**
     * 
     * @param parent 
     * @param modal 
     * @param nombreUsuario 
     */
      public VentanaModificarPrograma(java.awt.Frame parent, boolean modal, String nombreUsuario) {
        super(parent, modal);
        conectar();
        initComponents();     
          jTUsuario.setText(nombreUsuario);
          cargarPrograma();
        dimensionarPantalla();       
    }
    /**
     * Meotod que centra la ventana en el monitor
     */
    	private void dimensionarPantalla()
  	{
  		Dimension pantalla, cuadro;
  		
		pantalla = Toolkit.getDefaultToolkit().getScreenSize();
		cuadro = this.getSize();				
		
		this.setLocation(((pantalla.width - cuadro.width)/2), 
						(pantalla.height - cuadro.height)/2);
	}
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTNombre = new javax.swing.JTextField();
        codprograma = new javax.swing.JComboBox();
        java.util.Vector datos = datbase.consultaCampos("select progcodigo from programas order by progcodigo asc");
        for (int i=0; i< datos.size();i++)
        codprograma.addItem(datos.elementAt(i));
        new S15WorkingBackspace(codprograma);
        botonGuardar = new javax.swing.JButton();
        botonBuscar = new javax.swing.JButton();
        botonCerrar = new javax.swing.JButton();
        jPEncabezado = new javax.swing.JPanel();
        JTFecha = new javax.swing.JTextField();
        jTUsuario = new javax.swing.JTextField();
        jLTitulo = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLlogo = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLEstado = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Programas acad\u00e9micos"));
        jLabel1.setText("C\u00f3digo");

        jLabel2.setText("Nombre");

        jTNombre.setToolTipText("Nombre del programa acad\u00e9mico");
        jTNombre.setDocument(new everest3.utils.LimitadorCaracteres(jTNombre,100,2));
        jTNombre.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jTNombreMouseEntered(evt);
            }
        });
        jTNombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTNombreActionPerformed(evt);
            }
        });
        jTNombre.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTNombreFocusGained(evt);
            }
        });

        codprograma.setEditable(true);
        codprograma.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                codprogramaMouseEntered(evt);
            }
        });
        codprograma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                codprogramaActionPerformed(evt);
            }
        });
        codprograma.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                codprogramaFocusGained(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(17, 17, 17)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE)
                    .addComponent(codprograma, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(codprograma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        botonGuardar.setText("Modificar");
        botonGuardar.setToolTipText("Guardar programa acad\u00e9mico");
        botonGuardar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                botonGuardarMouseEntered(evt);
            }
        });
        botonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGuardarActionPerformed(evt);
            }
        });
        botonGuardar.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                botonGuardarFocusGained(evt);
            }
        });

        botonBuscar.setText("Ayuda");
        botonBuscar.setToolTipText("Buscar un programa acad\u00e9mico");
        botonBuscar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                botonBuscarMouseEntered(evt);
            }
        });
        botonBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonBuscarActionPerformed(evt);
            }
        });
        botonBuscar.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                botonBuscarFocusGained(evt);
            }
        });

        botonCerrar.setText("Cerrar");
        botonCerrar.setToolTipText("Cierra esta ventana");
        botonCerrar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                botonCerrarMouseEntered(evt);
            }
        });
        botonCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCerrarcerrarActionPerformed(evt);
            }
        });
        botonCerrar.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                botonCerrarFocusGained(evt);
            }
        });

        jPEncabezado.setLayout(null);

        jPEncabezado.setBackground(new java.awt.Color(204, 204, 204));
        JTFecha.setEditable(false);
        JTFecha.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        JTFecha.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        JTFecha.setDragEnabled(true);
        JTFecha.setEnabled(false);
        JTFecha.setFocusable(false);
        JTFecha.setText(FechaAct.obtenerFechaCompleta());
        jPEncabezado.add(JTFecha);
        JTFecha.setBounds(350, 10, 160, 20);

        jTUsuario.setEditable(false);
        jTUsuario.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jTUsuario.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTUsuario.setDragEnabled(true);
        jTUsuario.setEnabled(false);
        jPEncabezado.add(jTUsuario);
        jTUsuario.setBounds(350, 40, 160, 20);

        jLTitulo.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLTitulo.setText("rogramas Acad\u00e9micos");
        jPEncabezado.add(jLTitulo);
        jLTitulo.setBounds(115, 20, 200, 22);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 48));
        jLabel3.setText("P");
        jPEncabezado.add(jLabel3);
        jLabel3.setBounds(90, 0, 32, 40);

        jLlogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/everest3/imagenes/logo.JPG")));
        jLlogo.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        jLlogo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jPEncabezado.add(jLlogo);
        jLlogo.setBounds(0, 0, 85, 64);

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel6.setText("Modificar");
        jPEncabezado.add(jLabel6);
        jLabel6.setBounds(160, 40, 90, 22);

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/everest3/imagenes/Barra.jpg")));
        jPEncabezado.add(jLabel4);
        jLabel4.setBounds(0, 0, 530, 65);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(botonGuardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonCerrar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonBuscar))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addComponent(jPEncabezado, javax.swing.GroupLayout.PREFERRED_SIZE, 517, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 517, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(jLEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 392, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(94, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPEncabezado, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonBuscar)
                    .addComponent(botonCerrar)
                    .addComponent(botonGuardar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLEstado, javax.swing.GroupLayout.DEFAULT_SIZE, 19, Short.MAX_VALUE))
        );
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-525)/2, (screenSize.height-279)/2, 525, 279);
    }// </editor-fold>//GEN-END:initComponents

    private void botonCerrarMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botonCerrarMouseEntered
// TODO add your handling code here:
         barraEstado("Cierra esta Ventana y Vuelve al Menu Principal");
    }//GEN-LAST:event_botonCerrarMouseEntered

    private void botonCerrarFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_botonCerrarFocusGained
// TODO add your handling code here:
         barraEstado("Cierra esta Ventana y Vuelve al Menu Principal");
    }//GEN-LAST:event_botonCerrarFocusGained

    private void botonBuscarMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botonBuscarMouseEntered
// TODO add your handling code here:
         barraEstado("Visualiza la Ayuda del Sistema");
    }//GEN-LAST:event_botonBuscarMouseEntered

    private void botonBuscarFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_botonBuscarFocusGained
// TODO add your handling code here:
         barraEstado("Visualiza la Ayuda del Sistema");
    }//GEN-LAST:event_botonBuscarFocusGained

    private void botonGuardarMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botonGuardarMouseEntered
// TODO add your handling code here:
         barraEstado("Guarda el Nuevo Programa Acad\u00e9mico en el Sistema");
    }//GEN-LAST:event_botonGuardarMouseEntered

    private void botonGuardarFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_botonGuardarFocusGained
// TODO add your handling code here:
         barraEstado("Guarda el Nuevo Programa Acad\u00e9mico en el Sistema");
    }//GEN-LAST:event_botonGuardarFocusGained

    private void jTNombreMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTNombreMouseEntered
// TODO add your handling code here:
         barraEstado("Digita el Nombre del Programa Acad\u00e9mico (Maximo 100 Caracteres)");
    }//GEN-LAST:event_jTNombreMouseEntered

    private void jTNombreFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTNombreFocusGained
// TODO add your handling code here:
          barraEstado("Digita el Nombre del Programa Acad\u00e9mico (Maximo 100 Caracteres)");
    }//GEN-LAST:event_jTNombreFocusGained

    private void codprogramaMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_codprogramaMouseEntered
// TODO add your handling code here:
         barraEstado("Selecciona el Codigo del Programa Acad\u00e9mico");
    }//GEN-LAST:event_codprogramaMouseEntered

    private void codprogramaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_codprogramaFocusGained
// TODO add your handling code here:
        barraEstado("Selecciona el Codigo del Programa Acad\u00e9mico");
    }//GEN-LAST:event_codprogramaFocusGained

    private void botonBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonBuscarActionPerformed
// TODO add your handling code here:
         new MostrarAyuda();
    }//GEN-LAST:event_botonBuscarActionPerformed

    private void codprogramaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_codprogramaActionPerformed
// TODO add your handling code here:
         cargarPrograma();
    }//GEN-LAST:event_codprogramaActionPerformed

    private void botonCerrarcerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCerrarcerrarActionPerformed
         this.dispose();
    }//GEN-LAST:event_botonCerrarcerrarActionPerformed

    private void botonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonGuardarActionPerformed
// TODO add your handling code here:
        validaDatos();
    }//GEN-LAST:event_botonGuardarActionPerformed

    private void jTNombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTNombreActionPerformed
// TODO add your handling code here:
        validaDatos();
    }//GEN-LAST:event_jTNombreActionPerformed
    
    private void cargarPrograma()
    {
         if (codprograma.getSelectedIndex()!=-1){
           try{
            jTNombre.setText((String) datbase.ConsultaIndividual("select prognombre from programas where progcodigo ="+codprograma.getSelectedItem()).get(0).toString().trim().toUpperCase());   
           }
           catch (Exception e) {
               javax.swing.JOptionPane.showMessageDialog(this,"ERROR PARAMETRO NO ENCONTRADO","ERROR!!",javax.swing.JOptionPane.ERROR_MESSAGE);
               
           }
            
        }
    }
      /**
     * metodo que envia a la clase  JDBCAdapter, el conector para la base dee datos, el nombre del
     * administrador de la misma, la clave y el driver.
     */
    private void conectar() {
        datbase = new JDBCAdapter(
            "jdbc:postgresql://localhost:5432/everest",
            "org.postgresql.Driver",
            "postgres",
            "root");
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
              public void run() {
                    try{
                javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
            }
            catch(Exception e){
                
            }
                new VentanaModificarPrograma(new javax.swing.JFrame(), true).setVisible(true);
            }
        });
    }

    private void validaDatos() {
        NuevoPrograma newProgram = new NuevoPrograma();
        newProgram.actualizaPrograma(codprograma.getItemAt(codprograma.getSelectedIndex()).toString(),jTNombre.getText());
         cargarPrograma();
//        this.dispose();
      //  limpiar();
    }
    
    private void limpiar(){
        
        codprograma.setSelectedIndex(0);
        jTNombre.setText("");
        codprograma.requestFocus();
    }
     private void barraEstado(String mensaje)
      {
          jLEstado.setText(mensaje);
      }
     JDBCAdapter datbase;   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField JTFecha;
    private javax.swing.JButton botonBuscar;
    private javax.swing.JButton botonCerrar;
    private javax.swing.JButton botonGuardar;
    private javax.swing.JComboBox codprograma;
    private javax.swing.JLabel jLEstado;
    private javax.swing.JLabel jLTitulo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLlogo;
    private javax.swing.JPanel jPEncabezado;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTNombre;
    private javax.swing.JTextField jTUsuario;
    // End of variables declaration//GEN-END:variables
    Fecha FechaAct= new Fecha(); 
}
